  //1 声明变量用来缓存组件对象
var authoDatagrid, authoSearchDialog, authoSearchForm, authoDialog, authoDialogForm, querySystemDialog, querySystemForm;
$(function () {
  //2 使用声明的变量来缓存组件
  authoDatagrid = $("#authoDatagrid");
  authoSearchDialog = $("#authoSearchDialog");
  authoSearchForm = $("#authoSearchForm");
  authoDialog = $("#authoDialog");
  authoDialogForm = $("#authoDialogForm");
  querySystemDialog = $("#querySystemDialog");
  querySystemForm = $("#querySystemForm");


  //3 创建一个命令对象，存放所有的按钮的点击事件的回调
  cmdObj = {
    authoRefresh: function () {
      $("#searchQ").textbox("setValue", "");
      authoDatagrid.datagrid("reload");
    },
    authoSystemCancel: function () {
      authoDialog.dialog("close");
    },
    search: function () {
      authoDatagrid.datagrid("load", $("#searchForm").serializeJSON())
    },
    searchAdvance: function () {
      authoSearchForm.form("clear");
      authoSearchDialog.dialog("open").dialog("center");
    },
    searchCommit: function () {
      //封装参数
      var param = authoSearchForm.serializeJSON();
      //console.debug(param);
      //关闭模态框
      authoSearchDialog.dialog("close");
      // 重新加载数据表格
      authoDatagrid.datagrid("load", param);
    },
    searchCancel: function () {
      authoSearchDialog.dialog("close");
    },
    authoSystem: function () {
      //获取选中行
      var row = authoDatagrid.datagrid("getSelected");
      //判断是否选中
      if (!row) {
        $.messager.alert("提示", "请选中一个系统进行申请", "info");
        return;
      }
      //清空表单
      authoDialogForm.form("clear");
      authoDialogForm.form("load", row);
      authoDialog.dialog("open").dialog("center");
    },
    authoSystemCommit: function () {
      //封装参数
      var param = authoDialogForm.serializeJSON();
      console.debug(param);
      authoDialogForm.form('submit', {
          url: "autho/startProcessInstance",
          onsubmit: function () {
            return authoDialogForm.form("validate");
          },
          success: function (data) {
            var result = $.parseJSON(data);
            console.debug(result);
            if (result.success) {
              //关闭模态框
              authoDialog.dialog("close");
              //给出提示
              $.messager.alert("提示", "申请已提交", "info");
            } else {
              //给出失败提示
              $.messager.alert("提示", result.message, "error");
            }
          }
        }
      )
    },
    queryScode: function () {
      querySystemForm.form("clear");
      querySystemDialog.dialog("open").dialog("center");
    },
    queryAgain:function () {
      querySystemForm.form('submit',{
        url:'autho/queryApply',
        onsubmit:function () {
          return querySystemForm.form('validate');
        },
        success:function (data) {
          //console.debug(data);
          var result = $.parseJSON(data);
          // console.debug(result);
          // console.debug(result.task);
          // console.debug(result.task.state);
          if(result.success&&result.task!=null&&result.task.state==2){
            authoDialogForm.form("clear");
            authoDialogForm.form('load',result.task);
            authoDialog.dialog("open").dialog("center");
            querySystemDialog.dialog("close");
          }else{
            $.messager.alert("info","申请码填写错误或者已提交","error");
          }
        }
      })
    },
    //提交查询码进行查询
    queryCommit: function () {
      querySystemForm.form('submit', {
        url: "autho/queryApply",
        onsubmit: function () {
          return querySystemForm.form("validate");
        },
        success: function (data) {
          //console.debug(data);
          var result = $.parseJSON(data);
          //console.debug(aa);
          if (result.success) {
            querySystemDialog.dialog("close");
            if (result.denyrea)
              $.messager.alert("提示", result.scode +"---"+ result.denyrea, "info");
             else
              $.messager.alert("提示", result.scode, "info");
          } else {
            $.messager.alert("提示", result.message, "info");
          }
        }
      })
    },
    queryCancel: function () {
      //$.messager.alert("","1111","info");
      querySystemDialog.dialog("close");
    }
  };

  //4 初始化组件
  //4.1 初始化数据表格
  authoDatagrid.datagrid({
    title: '列表',
    url: 'system/page',
    pagination: true,
    rownumbers: true,
    singleSelect: true,
    fit: true,
    pageList: [10, 15, 20],
    pageSize: 10,
    fitColumns: true,//列宽度
    columns: [[
      {field: 'identification', title: '标识', width: 100},
      {field: 'name', title: '系统名称', width: 100},
      {field: 'scode', title: '编码', width: 100},
      {field: 'version', title: '版本', width: 100}
    ]],
    toolbar: '#authoDatagridToolbar',
  });

  //4.2初始化申请表单
  authoDialog.dialog({
    title: "系统申请单",
    iconCls: "icon-search",
    width: 450,
    height: 330,
    closed: true,
    modal: true,
    buttons: "#authoSystemDialogToolbar"
  });

  //4.3初始化高级查询模态框
  authoSearchDialog.dialog({
    title: "高级查询",
    iconCls: "icon-search",
    width: 400,
    height: 200,
    closed: true,
    modal: true,
    buttons: "#authosSearchDialogToolbar"
  });

  //4.4初始化系统查询模态框
  querySystemDialog.dialog({
    title: "申请码查询",
    iconCls: "icon-search",
    width: 400,
    height: 150,
    closed: true,
    modal: true,
    buttons: "#querySystemDialogToolbar"
  });

  //5 为页面上的所有的a标签注册点击事件
  $("a").on("click", function () {
    //获取标签的data-cmd属性值
    var cmd = $(this).data("cmd");

    if (cmd) {
      cmdObj[cmd + ""]();
    }
  });
});
